<template>
  <div>
    <PageDialog
      title="未绑定RFID码入库单"
      v-model:visible="shows"
      :bt-loading="dialogInfo.btLoading"
      :bt-list="dialogInfo.btList"
    >
      <new-page-filter
        v-model:query="filterInfo.query"
        :clear-type="clearType"
        :filter-list="filterInfo.searchList"
        :filter-btn="filterInfo.searchBtn"
      >
      </new-page-filter>
      <PageTable
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :listen-height="false"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :highlight-row="true"
        :check-box="false"
        :isDynamic="true"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-storageUserName="scope">
          <span v-if="formData.value === '1'">{{
            scope.row.storeUserName
          }}</span>
          <span v-if="formData.value === '2'">{{
            scope.row.storageUserName
          }}</span>
        </template>
        <template v-slot:col-storeTime="scope">
          <span v-if="formData.value === '1'">{{ scope.row.storeTime }}</span>
          <span v-if="formData.value === '2'">{{ scope.row.storageTime }}</span>
        </template>
      </PageTable>
    </PageDialog>
  </div>
</template>
<script setup name="addStorageOrder">
import { getDepotAll } from '@/api/core'
import { searchNoBindReceivingOrderList } from '@/api/smartCabinet'
const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})
const selectRow = ref('')
const formData = ref({
  value: '1',
})
const clearType = ref(true)

// 过滤相关配置
const filterInfo = ref({
  copyQuery: {},
  query: {
    ...props.query,
    sort: 'store_time desc',
  },
  searchList: [
    {
      componentType: 'input',
      label: '入库单号',
      value: 'orderNo',
      event: search,
    },
    {
      label: '入库仓库',
      value: 'depotName',
      asValue: 'depotIdMult',
      multiple: true,
      componentType: 'slip-select',
      query: {
        depotAuth: 'id',
        status: 1,
        useTypeId: '2',
      },
      api: getDepotAll,
      event: search,
    },
    {
      value: 'bindStatusValue',
      asValue: 'bindStatus',
      componentType: 'select-model',
      label: '绑定状态',
      checkDefault: false,
      event: search,
      clearable: true,
      code: 'rfid_bind_status',
    },
    {
      label: '入库人',
      value: 'storageUserName',
      asValue: 'storeUser',
      componentType: 'select-user',
      highlightRow: true,
      checkBox: false,
    },
    {
      componentType: 'datetimerange',
      label: '入库时间',
      value: 'storeTime',
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  api: searchNoBindReceivingOrderList,
  data: [],
  fieldList: [
    { label: '入库单号', value: 'orderNo' },
    { label: '入库仓库', value: 'depotName' },
    { label: '绑定状态', value: 'bindStatusValue' },
    { label: '入库人', value: 'storageUserName', type: 'slot' },
    { label: '入库时间', value: 'storeTime', type: 'slot' },
  ],
})
const dialogInfo = ref({
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    proxy.$emit('update:showDialog', val) // 更新父组件shows
  },
})
watch(
  () => filterInfo.value.query,
  (val) => {
    if (val && val.storeTime) {
      let times = val.storeTime.split(',')
      val['startTime'] = times[0]
      val['endTime'] = times[1]
    }
    if (!val.storeTime) {
      delete val.startTime
      delete val.endTime
    }
  },
  {
    deep: true,
  }
)
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  if (selectRow.value && selectRow.value.id) {
    handleClose()
    proxy.$emit('callback', selectRow.value)
  } else {
    proxy.$message({
      message: '请选择入库单',
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
  }
}
function handleClose() {
  shows.value = false
  proxy.$emit('reset')
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
function clearSearch() {
  filterInfo.value.query = proxy.$fn.deepClone(filterInfo.value.copyQuery)
  clearType.value = !clearType.value
  getList()
}
onMounted(() => {
  proxy.$nextTick(() => {
    getList()
  })
})
</script>
<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
